//
//  ContentViewController.m
//  FingerChinese
//
//  Created by luar on 09/02/2010.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ContentViewController.h"
#import "Content.h"
#import "GradientView.h"
#import "FingerChineseViewController.h"

@implementation ContentViewController

@synthesize contentList, cellWidth1, titleWidth1, cellWidth3, titleWidth3, fontSize_en, fontSize_cn, pageNo, isFlashCard;

static int iconWidth = 48;
static int arrowWidth = 30;

- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
		[self calculateLabelWidth];
		fontSize_en = 18;
		fontSize_cn = 14;
		self.view.backgroundColor = [UIColor clearColor];
		self.tableView.separatorColor = HEXCOLOR(0x92979Bff);
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
	//self.tableView.delegate = self;
	
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    //self.navigationItem.rightBarButtonItem = self.editButtonItem;
	//self.tableView.rowHeight = 48;

	/*UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu.png"] style:UIBarButtonItemStylePlain target:nil action:nil];
	self.navigationItem.backBarButtonItem = backButton;
	[backButton release];*/
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	[self updateInterfaceOrientation];
	return YES;
}

- (void)updateInterfaceOrientation {
	if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
		cellWidth = cellWidth3;
		titleWidth = titleWidth3;
		tableOrientation = 3;
	} else {
		cellWidth = cellWidth1;
		titleWidth = titleWidth1;
		tableOrientation = 1;
	}
	//NSLog(@"ori %i, %i, %i %i %i %i %i", tableOrientation, [UIDevice currentDevice].orientation, UIDeviceOrientationPortrait,  UIDeviceOrientationPortraitUpsideDown, UIDeviceOrientationLandscapeLeft, UIDeviceOrientationLandscapeRight);
	[self.tableView reloadData];	
}

- (void)calculateLabelWidth {
	cellWidth1 = [UIScreen mainScreen].bounds.size.width-iconWidth-arrowWidth;
	titleWidth1 = [UIScreen mainScreen].bounds.size.width-20;//-arrowWidth;
	cellWidth3 = [UIScreen mainScreen].bounds.size.height-iconWidth-arrowWidth;
	titleWidth3 = [UIScreen mainScreen].bounds.size.height-20;//-arrowWidth;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark -
#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [contentList count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	UILabel *title_en = nil;
	UILabel *title_cn = nil;
	UIImageView *icon = nil;
	
	Content *_content = [contentList objectAtIndex:indexPath.row];	
	int lbHeight_en = (tableOrientation==1) ? _content.lbHeight_en1:_content.lbHeight_en3;
	int lbHeight_cn = (tableOrientation==1) ? _content.lbHeight_cn1:_content.lbHeight_cn3;
	int cellHeight = (tableOrientation==1) ? _content.cellHeight1:_content.cellHeight3;
	
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		cell.backgroundView = [[[GradientView alloc] init] autorelease];
		title_en = [[[UILabel alloc] initWithFrame:cell.frame] autorelease];
		title_en.lineBreakMode = UILineBreakModeWordWrap;
		title_en.numberOfLines = 0;
		title_en.tag = 1;
		title_en.font = [UIFont systemFontOfSize:fontSize_en];
		title_en.highlightedTextColor = [UIColor whiteColor];
		title_en.backgroundColor = [UIColor clearColor];
		
		title_cn = [[[UILabel alloc] initWithFrame:cell.frame] autorelease];
		title_cn.lineBreakMode = UILineBreakModeWordWrap;
		title_cn.numberOfLines = 0;
		title_cn.tag = 2;
		title_cn.font = [UIFont systemFontOfSize:fontSize_cn];
		title_cn.highlightedTextColor = [UIColor whiteColor];
		title_cn.backgroundColor = [UIColor clearColor];
		
		icon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, iconWidth, cellHeight)];
		icon.contentMode = UIViewContentModeCenter;
		icon.tag = 3;
		
		[cell.contentView addSubview:icon];
		[cell.contentView addSubview:title_en];
		[cell.contentView addSubview:title_cn];
	} else {
		title_en = (UILabel *)[cell.contentView viewWithTag:1];
		title_cn = (UILabel *)[cell.contentView viewWithTag:2];
		icon = (UIImageView *)[cell.contentView viewWithTag:3];
	}
    
    // Set up the cell...
	title_en.text = _content.title_en;
	title_cn.text = _content.title_cn;

	if (_content.icon && !_content.isAlignCenter) {
		icon.image = [UIImage imageNamed:_content.icon];
		icon.center = CGPointMake(icon.center.x, cellHeight/2);
	} else {
		icon.image = nil;
	}
	//NSLog(@"icon %i %@ %f %f", indexPath.row, _content.icon, cell.frame.size.height, icon.center.y);

	if (!_content.isTitle) {
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		cell.selectionStyle = UITableViewCellSelectionStyleBlue;
		if (!_content.isAlignCenter) {
			title_en.textAlignment = title_cn.textAlignment = UITextAlignmentLeft;
			title_en.frame = CGRectMake(iconWidth, 3, cellWidth, lbHeight_en);
			title_cn.frame = CGRectMake(iconWidth, lbHeight_en+3, cellWidth, lbHeight_cn);			
		} else {
			title_en.textAlignment = title_cn.textAlignment = UITextAlignmentCenter;
			title_en.frame = CGRectMake(10, 3, titleWidth, lbHeight_en);
			title_cn.frame = CGRectMake(10, lbHeight_en+3, titleWidth, lbHeight_cn);			
		}
		title_en.textColor = [UIColor blackColor];
		title_cn.textColor = [UIColor grayColor];
		title_en.shadowColor = title_cn.shadowColor = [UIColor clearColor];
	} else {
		cell.accessoryType = UITableViewCellAccessoryNone;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		title_en.textAlignment = title_cn.textAlignment = UITextAlignmentCenter;
		title_en.frame = CGRectMake(10, 3, titleWidth, lbHeight_en);
		title_cn.frame = CGRectMake(10, lbHeight_en+3, titleWidth, lbHeight_cn);
		title_en.textColor = title_cn.textColor = [UIColor whiteColor];
		title_en.shadowColor = title_cn.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.35];
		//title_en.shadowOffset = title_cn.shadowOffset = CGSizeMake(-0.5, -1.0);
	}
	[cell.backgroundView changeColor:_content.isTitle];
	
	/*[title_cn release];
	[title_en release];
	[icon release];*/
	
    return cell;
}

// No need <UITableViewDelegate> in .h, no need setDelegate, too
// Since it is trigger when mouseUp, so selectionStyleNone still need
// so willSelectRowAtIndexPath is used for not trigger didSelectRowAtIndexPath event
/*- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSLog(@"willSelectRowAtIndexPath %i", indexPath.row);
	Content *_content = [contentList objectAtIndex:indexPath.row];
	if (!_content.isTitle) {
		return indexPath;
	} else {
		return nil;
	}
}*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
	NSLog(@"didSelectRowAtIndexPath %i", indexPath.row);
	Content *_content = [contentList objectAtIndex:indexPath.row];
	pageNo = _content.pageNo-1;
	isFlashCard = _content.isFlashCard;
	if (pageNo >= 0) {
		NSLog(@"didSelectRowAtIndexPath %i %i", _content.isFlashCard, _content.pageNo);
		[[NSNotificationCenter defaultCenter] postNotificationName:@"MenuClick" object:self];
	}
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	Content *_content = [contentList objectAtIndex:indexPath.row];
	if (tableOrientation == 1) {
		return _content.cellHeight1;
	} else {
		return _content.cellHeight3;
	}
}


#pragma mark -

- (void)dealloc {
	[contentList release];
    [super dealloc];
}


@end

